package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.ProblemReplyOriginal;
import com.isunland.managesystem.utils.FileUtil;

/**
 * Created by lenovo on 2016/11/17.
 */
public class ProblemReplyDetailFragment extends Fragment implements View.OnClickListener {

  private BaseVolleyActivity mActivity;
  private ProblemReplyOriginal.ProblemReplyContent content;
  private TextView mTvReply;
  private TextView mTvRemark;
  private TextView mTvReplyPerson;
  private TextView mTvReplyTime;
  private CheckBox mCbIfFinish;
  private TextView mTvRp1;
  private TextView mTvRp2;
  private TextView mTvRp3;
  private TextView mTvRp4;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.reply_detail);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    mActivity = (BaseVolleyActivity) getActivity();
    content = (ProblemReplyOriginal.ProblemReplyContent) getArguments().getSerializable(
        ProblemReplyListFragment.EXTRA_VALUE);
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_problem_reply, container, false);
    findViewById(view);
    initData();
    return view;
  }

  private void initData() {
    mTvReply.setText(content.getReply());
    mTvRemark.setText(content.getRemark());
    mTvReplyPerson.setText(content.getRegStaffName());
    mTvReplyTime.setText(content.getRegDate());
    if ("T".equalsIgnoreCase(content.getIfFinish())) {
      mCbIfFinish.setChecked(true);
    } else if ("F".equalsIgnoreCase(content.getIfFinish())) {
      mCbIfFinish.setChecked(false);
    }
    mCbIfFinish.setEnabled(false);
    if (!TextUtils.isEmpty(content.getResourcePath1())) {
      mTvRp1.setVisibility(View.VISIBLE);
      mTvRp1.setText(FileUtil.getFileNameByFilePath(content.getResourcePath1()));
    }
    if (!TextUtils.isEmpty(content.getResourcePath2())) {
      mTvRp2.setVisibility(View.VISIBLE);
      mTvRp2.setText(FileUtil.getFileNameByFilePath(content.getResourcePath2()));
    }
    if (!TextUtils.isEmpty(content.getResourcePath3())) {
      mTvRp3.setVisibility(View.VISIBLE);
      mTvRp3.setText(FileUtil.getFileNameByFilePath(content.getResourcePath3()));
    }
    if (!TextUtils.isEmpty(content.getResourcePath4())) {
      mTvRp4.setVisibility(View.VISIBLE);
      mTvRp4.setText(FileUtil.getFileNameByFilePath(content.getResourcePath4()));
    }
    //附件下载
    mTvRp1.setOnClickListener(this);
    mTvRp2.setOnClickListener(this);
    mTvRp3.setOnClickListener(this);
    mTvRp4.setOnClickListener(this);
  }

  private void findViewById(View view) {

    mTvReply = (TextView) view.findViewById(R.id.tv_reply);
    mTvRemark = (TextView) view.findViewById(R.id.tv_remark);
    mTvReplyPerson = (TextView) view.findViewById(R.id.tv_replyPerson);
    mTvReplyTime = (TextView) view.findViewById(R.id.tv_replyTime);
    mCbIfFinish = (CheckBox) view.findViewById(R.id.cb_ifFinish);
    mTvRp1 = (TextView) view.findViewById(R.id.tv_rp1);
    mTvRp2 = (TextView) view.findViewById(R.id.tv_rp2);
    mTvRp3 = (TextView) view.findViewById(R.id.tv_rp3);
    mTvRp4 = (TextView) view.findViewById(R.id.tv_rp4);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  public static final ProblemReplyDetailFragment newInstence(
      ProblemReplyOriginal.ProblemReplyContent content) {
    ProblemReplyDetailFragment mfragment = new ProblemReplyDetailFragment();
    Bundle bundle = new Bundle();
    bundle.putSerializable(ProblemReplyListFragment.EXTRA_VALUE, content);
    mfragment.setArguments(bundle);
    return mfragment;
  }

  @Override public void onClick(View view) {
    switch (view.getId()) {
      //图片一
      case R.id.tv_rp1:
        downFile(content.getResourcePath1());
        break;
      case R.id.iv_extra1:
        break;
      //图片二
      case R.id.tv_rp2:
        downFile(content.getResourcePath2());
        break;
      case R.id.iv_extra2:
        break;
      //其他一
      case R.id.tv_rp3:
        downFile(content.getResourcePath3());
        break;
      case R.id.iv_extra3:
        break;
      //其他二
      case R.id.tv_rp4:
        downFile(content.getResourcePath4());
        break;
      case R.id.iv_extra4:
        break;
    }
  }

  private void downFile(String serverPath) {
    if (!TextUtils.isEmpty(serverPath)) {
      ExtraDownLoadDialogFragment dialog = ExtraDownLoadDialogFragment.newInstance("", serverPath);
      FragmentManager fm = getActivity().getSupportFragmentManager();
      dialog.show(fm, "");
    }
  }
}
